|
1
|
|
|
import { Injectable, Optional } from "@angular/core"; |
|
2
|
|
|
import { User as FirebaseUser } from "firebase"; |
|
3
|
|
|
import { AnonymousAuth } from "../modules/anonymous/anonymous-auth"; |
|
4
|
|
|
import { EmailAuth } from "../modules/email/email-auth"; |
|
5
|
|
|
import { FacebookAuth } from "../modules/facebook/facebook-auth"; |
|
6
|
|
|
import { GithubAuth } from "../modules/github/github-auth"; |
|
7
|
|
|
import { GoogleAuth } from "../modules/google/google-auth"; |
|
8
|
|
|
import { PhoneAuth } from "../modules/phone/phone-auth"; |
|
9
|
|
|
import { TwitterAuth } from "../modules/twitter/twitter-auth"; |
|
10
|
|
|
import { IAuthProvider } from "./i-auth-provider"; |
|
11
|
|
|
|
|
12
|
|
|
@Injectable({ |
|
13
|
|
|
providedIn: "root", |
|
14
|
|
|
}) |
|
15
|
|
|
export class AuthProvider { |
|
16
|
|
|
public constructor( |
|
17
|
|
|
@Optional() public authAnonymous: AnonymousAuth, |
|
18
|
|
|
@Optional() public authEmail: EmailAuth, |
|
19
|
|
|
@Optional() public authFacebook: FacebookAuth, |
|
20
|
|
|
@Optional() public authGithub: GithubAuth, |
|
21
|
|
|
@Optional() public authGoogle: GoogleAuth, |
|
22
|
|
|
@Optional() public authPhone: PhoneAuth, |
|
23
|
|
|
@Optional() public authTwitter: TwitterAuth, |
|
24
|
|
|
) {} |
|
25
|
|
|
|
|
26
|
|
|
public getProviderById( |
|
27
|
|
|
providerId: string | null | undefined, |
|
28
|
|
|
): IAuthProvider | null { |
|
29
|
|
|
switch (providerId) { |
|
30
|
|
|
case "password": |
|
31
|
|
|
return this.authEmail; |
|
32
|
|
|
case "phone": |
|
33
|
|
|
return this.authPhone; |
|
34
|
|
|
case "facebook.com": |
|
35
|
|
|
return this.authFacebook; |
|
36
|
|
|
case "github.com": |
|
37
|
|
|
return this.authGithub; |
|
38
|
|
|
case "google.com": |
|
39
|
|
|
return this.authGoogle; |
|
40
|
|
|
case "twitter.com": |
|
41
|
|
|
return this.authTwitter; |
|
42
|
|
|
case "apple.com": |
|
43
|
|
|
case "yahoo.com": |
|
44
|
|
|
case "hotmail.com": |
|
45
|
|
|
throw new Error(`Provider ${providerId} not implemented!`); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public getProvidersByUser(user: FirebaseUser): IAuthProvider[] { |
|
52
|
|
|
if (user.isAnonymous) { |
|
53
|
|
|
return [this.authAnonymous]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
const providers: IAuthProvider[] = []; |
|
57
|
|
|
|
|
58
|
|
|
for (const providerData of user.providerData) { |
|
59
|
|
|
if (providerData) { |
|
60
|
|
|
const provider = this.getProviderById(providerData.providerId); |
|
61
|
|
|
if (provider) { |
|
62
|
|
|
providers.push(provider); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return providers; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|